package com.itheima.servlet.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

// 演示响应字符数据
@WebServlet("/resp03")
public class ResponseDemo03 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.设置输出流的编码为utf-8(经验:设置编码的代码要放在前面)
        // 2.并告诉浏览器编码为utf-8,告诉浏览器返回的是网页
        response.setContentType("text/html;charset=utf-8");

        // 获取字符输出流
        PrintWriter writer = response.getWriter(); // 通过response得到的字符输出流默认是ISO-8859-1编码
        // 写数据到流中
        writer.write("<h1 align='center'>你好呀!</h1>");

        // 细节：流不需要关闭,Web服务器会处理
    }
}
